﻿using StockModel;
using TechnicalIndicators.Points;
using Utils;

namespace TechnicalIndicators.QuotationBased
{
	public class VolumeGenerator : GeneratorBase<Quotation, DatedSeriePoint>
	{
		#region Attributes

		private Quotation _lastProcessedQuotation;

		#endregion

		#region Public Methods

		public VolumeGenerator()
		{
			
		}

		public VolumeGenerator(ObservableCollection2<DatedSeriePoint> destination) : base(destination)
		{
		}

		protected override DatedSeriePoint CreatePoint(Quotation quotation)
		{
			DatedSeriePoint point;

			if (_lastProcessedQuotation != null && quotation.Volume != _lastProcessedQuotation.Volume)
			{
				point = new DatedSeriePoint(quotation.Date, quotation.Volume - _lastProcessedQuotation.Volume);
			}
			else if (_lastProcessedQuotation == null && quotation.Volume > 0)
			{
				point = new DatedSeriePoint(quotation.Date, quotation.Volume);
			}
			else
			{
				point = new DatedSeriePoint(quotation.Date, 0);
			}

			_lastProcessedQuotation = quotation;

			return point;
		}

		public override void Clear()
		{
			_lastProcessedQuotation = null;
			base.Clear();
		}

		#endregion
	}
}
